﻿<div style="width:100%">
    <div style="display:flex;">
        <a href="@Url" class="@(IsShowResult?"":"ms-3")" style="@ImageMinWidth">
            @if (string.IsNullOrWhiteSpace(Model.Image) == false)
            {
                <img src="@Model.Image" alt="@Model.Name" style="height:50px;" />
            }
        </a>
   

        <div style="width: 100%; height: 50px; position: relative; display: flex; ">
            <div class="option-background" style="@($"height: 100%;position: absolute;width:{(IsShowResult?((double)Model.Count*100 / TotalCount).ToString("0"):0)}%;")">

            </div>
            <div style=" display: flex; width: 100%; justify-content: space-between; height: 100%; align-content: center; z-index: 2;" class="ps-3">
                <div style="display: flex; flex-direction: column; justify-content: center;">
                    <div class="fw-bold" style="display: flex; align-items: center;">
                        <div style="overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical;">@Model.Name</div>
                        @if (IsSeleted&&IsShowResult)
                        {
                            <i class="fa fa-check-circle fa-fw primary-textms-1"></i>
                        }
                    </div>
                    @if (IsShowResult)
                    {
                        <div style="font-size:small;">@(Model.Count+"票")</div>
                    }
                </div>
                @if (IsShowResult)
                {
                    <div style=" display: flex; align-content: center; flex-direction: column; justify-content: center; ">
                        <div class="pe-3">
                            @Percent
                        </div>
                    </div>
                }

            </div>
        </div>
    </div>
</div>

@code {
    [Parameter]
    public VoteOptionViewModel Model { get; set; }
    [Parameter]
    public bool IsShowResult { get; set; }
    [Parameter]
    public bool IsSeleted { get; set; }
    [Parameter]
    public string ImageMinWidth { get; set; }

    [Parameter]
    public long TotalCount { get; set; }

    public string Url { get; set; } = "";
    public string Percent { get; set; }

    protected override void OnInitialized()
    {
        Url = Model.Type switch
        {
            VoteOptionType.Text => null,
            VoteOptionType.Entry => "entries/index/" + Model.ObjectId,
            VoteOptionType.Article => "articles/index/" + Model.ObjectId,
            VoteOptionType.Periphery => "peripheries/index/" + Model.ObjectId,
            _ => null,
        };
        if (TotalCount != 0)
        {
            Percent = Math.Round(((double)Model.Count * 100 / TotalCount), 2, MidpointRounding.AwayFromZero).ToString() + "%";
        }
        else
        {
            Percent = "0%";
        }
        StateHasChanged();
    }
}
